<?php

namespace app_supplier\controllers;
use Yii;
use yii\helpers\Url;
use yii\web\Controller;
use app_supplier\models;
use yii\web\Response;
use app_supplier\models\LoginForm;
class UserController extends Controller
{
    public $layout = false;
    public function actionLogin()
    {
        $this->view->title = '登陆中心';
        if (!Yii::$app->user->isGuest) {
            return $this->redirect(['main/index']);
        }
        
        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $row = $model->login()) {
            Yii::$app->session->set('user_dealer', $row);
            return $this->redirect(['main/index']);
        }
        return $this->render('login', [
            'model' => $model,
        ]);
    }

    public function actionLogout()
    {
        if(Yii::$app->user->logout()) {
            if (Yii::$app->session->has('user_dealer'))
                Yii::$app->session->remove('user_dealer');
            return $this->redirect(['user/login']);
        }
    }

    public function returnAjax($data = [])
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        return $data;
    }

}
